import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Scanner;
import  ArraySort.*;

import static java.lang.Thread.sleep;


public class petTest {
    public static void arrayCutting(String[] Name, Integer[] Age, Double[] Weight, String line, int start) {
        int begin = 0;
        int end = 0;
        //第一个相同符号判断 '
        int judge1 = 0;
        //第二个相同符号判断 =
        int judge2 = 0;
        //第三个相同符号判断 ,
        int judge3 = 0;
        for (int i = 0; i < line.length(); i++) {
            //名字划分
            if (line.charAt(i) == '\'' && judge1 == 0) {
                begin = i + 1;
                judge1 = 1;
                judge2 = 1;
            } else if (line.charAt(i) == '\'' && judge1 == 1) {
                end = i;
                judge1 = 2;
                Name[start] = line.substring(begin, end);
            }


            if (line.charAt(i) == '=' && judge2 == 1) {
                begin = i + 1;
                judge3 = 1;
                judge2 = 2;
            }
            //体重划分
            else if(line.charAt(i) == '='&& judge2==2)
            {
                begin = i+1;
                judge2=3;
            }

            //年龄划分
            if (line.charAt(i) == ',' && judge3 == 1) {
                end = i;
                judge3 = 2;
                Age[start] = Integer.parseInt(line.substring(begin, end));

            }
            else if(line.charAt(i)=='}')
            {
                end=i;
                Weight[start]= Double.parseDouble(line.substring(begin,end));
            }
        }
    }

    public static void main(String[] args) {
        //创造容器
        Scanner scan=new Scanner(System.in);
        //判断输入 0时为读操作 1时为写操作
        System.out.println("0时为读操作 1时为写操作");
        int judge=scan.nextInt();

        //船创建输入输出流
        PrintWriter outputStream = null;
        Scanner inputStream = null;

        //姓名数组
        String []Name=new String[100];

        //年龄数组
        Integer []Age=new Integer[100];

        //体重数组
        Double []Weight=new Double[100];

        if(judge==1)
        {
            //读取换行符
            scan.nextLine();

            //输入要写出的文件名
            System.out.println("Enter the file you want to output :");
            String fileName = scan.nextLine();
            try
            {
                outputStream=new PrintWriter(fileName);
            }
            catch (FileNotFoundException e) {
                System.out.println("Error opening the file" + " "+fileName);
                System.exit(0);
            }
            int number=0;
            System.out.println("How many records do you want to write in ?");

            number=scan.nextInt();
            Pet arr[]=new Pet[number+1];

            for(int i=0;i<number;i++)
            {
                System.out.println("Enter the pet's name :");
//                outputStream.println("Enter the pet's name :");
                scan.nextLine();
                String name=scan.nextLine();

                System.out.println("Enter the pet's age :");
//                outputStream.println("Enter the pet's age :");

                int age=scan.nextInt();

                System.out.println("Enter the pet's weight: ");
//                outputStream.println("Enter the pet's weight: ");

                double weight=scan.nextDouble();
                arr[i] = new Pet(name,age,weight);
                outputStream.println(arr[i].toString());

            }
            outputStream.close();
        }
        //文件存放位置
        if(judge==0) {
            //读取换行符
            scan.nextLine();

            //输入要读取的文件名
            System.out.println("Enter the file you want to input :");
            String fileName = scan.nextLine();



            //排序
            BubbleSort test=new BubbleSort();

            //判别
            try {
                inputStream = new Scanner(new File(fileName));
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Error opening the file" + " "+fileName);
                System.exit(0);
            }

            //进行读取操作
            int start=0;
            while(inputStream.hasNextLine())
            {

                String line= inputStream.nextLine();
                try {

                    sleep(1000); //暂停，每一秒输出一次

                }catch (InterruptedException e) {
                    return;
                }
                System.out.println(line);

                arrayCutting(Name,Age,Weight,line,start);
                start++;
            }

            //对象数组
            Pet[]pet=new Pet[start];

            for(int i=0;i<start;i++)
            {
                pet[i]=new Pet(Name[i],Age[i],Weight[i]);
            }

            //年龄排序
            test.bubbleSort(pet, new Comparator<Comparable>() {//第二种参数
                @Override
                public int compare(Comparable o1, Comparable o2) {
                    return ((Pet) o1).getAge().compareTo(((Pet) o2).getAge());
                }
            });
            System.out.println("The youngest pet: "+pet[0].getName()+" "+pet[0].getAge());
            System.out.println("The oldest pet: "+pet[start-1].getName()+" "+pet[start-1].getAge());


            //重量排序
            test.bubbleSort(pet, new Comparator<Comparable>() {//第二种参数
                @Override
                public int compare(Comparable o1, Comparable o2) {
                    return ((Pet) o1).getWeight().compareTo(((Pet) o2).getWeight());
                }
            });
            System.out.println("The fattest pet: "+pet[0].getName() +" "+pet[0].getWeight());
            System.out.println("The lightest pet: "+pet[start-1].getName()+" "+pet[start-1].getWeight());
            //测试案例
            inputStream.close();
        }


    }


}
